<template>
	<div>
		<el-form label-width="120px" :model="upload.params">
			<el-form-item label="处置单位名称">
				<el-input v-model="upload.params.disposeEntName" class="element-width" placeholder="请填写处置单位名称">
				</el-input>
			</el-form-item>
			<el-form-item label="选择废物">
				<el-select v-model="upload.params.wasteIdList" filterable placeholder="请选择" multiple
					class="element-width">
					<el-option v-for="item in wasteList" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="开始时间" prop="startTime">
				<el-date-picker class="element-width" v-model="upload.params.startTime" type="date"
					placeholder="选择开始时间">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="截止时间" prop="endTime">
				<el-date-picker class="element-width" v-model="upload.params.endTime" type="date" placeholder="选择截止时间">
				</el-date-picker>
			</el-form-item>
		</el-form>
		<div style="padding: 0 36px;margin-bottom: 10px;">
			<el-upload accept=".pdf" :limit="1" :multiple="false" :file-list="upload.businessFileList" action="#"
				:auto-upload="false" :on-change="changebusinessFile" :on-remove="removebusinessFile">
				<el-button type="text" style="margin: 0 0 -15px 0">上传营业执照文件</el-button>
			</el-upload>
		</div>

		<div style="padding: 0 36px;margin-bottom: 10px;">
			<el-upload accept=".pdf" :limit="1" :multiple="false" :file-list="upload.permissionFileList" action="#"
				:auto-upload="false" :on-change="changepermissionFile" :on-remove="removepermissionFile">
				<el-button type="text" style="margin: 0 0 -15px 0">上传经营许可证文件</el-button>
			</el-upload>
		</div>

		<div style="padding: 0 36px;margin-bottom: 10px;">
			<el-upload accept=".pdf" :limit="1" :multiple="false" :file-list="upload.orderFileList" action="#"
				:auto-upload="false" :on-change="changeorderFile" :on-remove="removeorderFile">
				<el-button type="text" style="margin: 0 0 -15px 0">上传合同文件</el-button>
			</el-upload>
		</div>
		<div style="text-align: center!important;margin-top: 30px;">
			<el-button type="primary" size="small" @click="save()" :loading="isLoading">确 定</el-button>
			<el-button size="small" @click="cancle">取 消</el-button>
		</div>
	</div>
</template>

<script>
	import $ from 'jquery'
	let that
	let APPKEY = '5da5441f62e48aedc7a3853ffc75c2db';
	let PRODID = 'gf';
	let UPLOAD_ACTION = '/upload';
	let VIEW_ACTION = '/view';
	let REMOVE_ACTION = '/remove';
	let IMGURL = 'https://www.weifeibangbang.com/wfbb-webupload/service/sys/file/upload';
	export default {
		name: 'uploadHtZz',
		components: {},
		props: ['selectItem'],
		data() {
			return {
				isLoading: false,
				params: {
					facilitatorId: '',
					categoryMainId: '',
					ticketId: '',
					itemId: ''
				},
				upload: {
					businessFile: '',
					businessFileList: [],
					permissionFile: '',
					permissionFileList: [],
					orderFile: '',
					orderFileList: [],

					visible: false,
					params: {
						disposeEntName: '',
						id: '', // 档案id
						facilitatorId: '', // 客户id
						categoryItemId: '', // 子类id
						businessLicenseFileId: '', // 文件id
						permissionFileId: '', //经营许可证文件id
						orderFileId: '', //合同Id
						endTime: '', // 到期时间
						startTime: '',
						wasteIdList: [],
					}
				},
				wasteList: []
			}
		},
		created: function() {
			console.log(this.selectItem)
		},
		mounted: function() {
			that = this
			that.params.facilitatorId = that.getQueryString("facilitatorId")
			that.params.categoryMainId = that.getQueryString("categoryMainId")
			that.params.ticketId = that.$store.state.token
			that.params.itemId = that.getQueryString("itemId")

			that.ajax({
				url: 'documentOrder/getImpactAssessmentWasteList?ticketId=' + that.params.ticketId,
				data: JSON.stringify({
					facilitatorId: that.params.facilitatorId,
					categoryType: that.selectItem.categoryType,
				}),
				contentType: 'application/json',
				dataType: 'json',
				method: 'POST',
				success: function(result) {
					if (result.status == 1) {
						for (let item of result.data) {
							item.label = item.wasteCode + "    " + item.wasteName
							item.value = item.id
						}
						that.wasteList = result.data
					}
				}
			});
		},
		methods: {
			cancle() {
				that.$emit("cancle", true)
			},

			changebusinessFile: function(file, fileList) {
				var _this = this;
				file.url = URL.createObjectURL(file.raw);
				if (file.raw.type.indexOf("pdf") == -1) {
					return this.$message({
						message: '请上传pdf文件',
						type: 'warning'
					});
				}
				_this.upload.businessFileList = fileList
				_this.upload.businessFile = file;
			},

			removebusinessFile() {
				this.upload.businessFileList = []
				this.upload.businessFile = ''
			},

			changepermissionFile: function(file, fileList) {
				var _this = this;
				file.url = URL.createObjectURL(file.raw);
				if (file.raw.type.indexOf("pdf") == -1) {
					return this.$message({
						message: '请上传pdf文件',
						type: 'warning'
					});
				}
				_this.upload.permissionFileList = fileList
				_this.upload.permissionFile = file;
			},

			removepermissionFile() {
				this.upload.permissionFileList = []
				this.upload.permissionFile = ''
			},

			changeorderFile: function(file, fileList) {
				var _this = this;
				file.url = URL.createObjectURL(file.raw);
				if (file.raw.type.indexOf("pdf") == -1) {
					return this.$message({
						message: '请上传pdf文件',
						type: 'warning'
					});
				}
				_this.upload.orderFileList = fileList
				_this.upload.orderFile = file;
			},

			removeorderFile() {
				this.upload.permissionFileList = []
				this.upload.orderFile = ''
			},

			removeFile() {
				this.upload.businessFileList = []
				this.upload.businessFile = ''
				this.upload.permissionFileList = []
				this.upload.permissionFile = ''
				this.upload.orderFileList = []
				this.upload.orderFile = ''
			},

			uploadFile: function(callback, fileItem) {
				var _this = this;
				var formdata = new FormData()
				formdata.append("file", fileItem.raw);
				formdata.append("appKey", APPKEY);
				formdata.append("businessCode", that.randomChar(19));
				formdata.append("prodID", PRODID);
				$.ajax({
					url: IMGURL + '/upload',
					data: formdata,
					type: 'POST',
					dataType: 'json',
					contentType: false,
					processData: false,
					success: function(result) {
						var fileId = result.message.fileID;
						callback(fileId);
					},
					error: function() {}
				})
			},


			save() {
				let that = this
				if (that.upload.disposeEntName == '') {
					return this.$message({
						message: '请填写标题',
						type: 'warning'
					});
				} else if (that.upload.businessFile == '') {
					return this.$message({
						message: '请上传营业执照文件',
						type: 'warning'
					});
				} else if (that.upload.permissionFile == '') {
					return this.$message({
						message: '请上传经营许可证文件',
						type: 'warning'
					});
				} else if (that.upload.orderFile == '') {
					return this.$message({
						message: '请上传合同文件',
						type: 'warning'
					});
				} else if (that.upload.params.wasteIdList.length == 0) {
					return this.$message({
						message: '请选择废物列表',
						type: 'warning'
					});
				}
				that.isLoading = true
				that.uploadFile(function(businessLicenseFileId) {
					that.uploadFile(function(permissionFileId) {
						that.uploadFile(function(orderFileId) {
							that.ajax({
								url: '/documentOrder/saveOrUpdateDocumentEntCategoryOrder?ticketId=' +
									that.params.ticketId,
								dataType: 'json',
								contentType: 'application/json',
								data: JSON.stringify({
									facilitatorId: that.params.facilitatorId,
									categoryItemId: that.selectItem.id,
									startTime: that.upload.params
										.startTime, // 开始时间
									endTime: that.upload.params.endTime, // 结束时间
									disposeEntName: that.upload.params
										.disposeEntName, // 处置单位名称
									businessLicenseFileId: businessLicenseFileId, // 处置单位名称文件id
									permissionFileId: permissionFileId, // 经营许可证文件id
									orderFileId: orderFileId, // 合同文件id
									wasteIdList: that.upload.params.wasteIdList
								}),
								success: function(result) {
									that.upload = that.$options.data().upload
									that.isLoading = false
									that.$emit("success", true)
									return that.$message({
										message: '上传成功！',
										type: 'success'
									});
								}
							})
						}, that.upload.orderFile)
					}, that.upload.permissionFile)
				}, that.upload.businessFile)
			},

			randomChar: function(l) {
				var x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
				var tmp = "";
				var timestamp = new Date().getTime();
				for (var i = 0; i < l; i++) {
					tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
				}
				return timestamp + tmp;
			},

		},
	}
</script>

<style>
	.element-width {
		width: 500px;
	}
</style>
